package com.prototype.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.prototype.po.Admin;
import com.prototype.service.AdminSerice;
import com.prototype.util.JqGrid;
import com.prototype.web.controller.BaseController;
import com.prototype.web.vo.AdminVO;

/**
 * 操作员管理Controller
 * 
 * @author eg366
 * 
 */
@Controller
@RequestMapping("/admin/operator")
public class AdminOperatorController extends BaseController {

	@Autowired
	private AdminSerice adminService;

	/**
	 * 管理员管理列表页
	 * 
	 * @return
	 */
	@RequestMapping("/list.htm")
	public String list() {
		return "admin.operator.list.def";
	}

	/**
	 * 管理员列表查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/query.htm")
	@ResponseBody
	public JqGrid query(String userName, String loginName) {
		/* 查询条件 */
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(userName))
			params.put("userName", userName);
		if (StringUtils.isNotEmpty(loginName))
			params.put("loginName", loginName);

		JqGrid jqGrid = getJqGridFromRequest();
		/* 查询用户总数量 */
		int recordNum = jqGrid.getRp();
		int count = adminService.getCountByParams(params);
		int startNum = jqGrid.init(count);
		List<AdminVO> result = adminService.listGrid(params, startNum,
				recordNum);
		jqGrid.setRows(result);
		jqGrid.setRecords(count);
		return jqGrid;
	}

	/**
	 * 新增管理员
	 */
	@RequestMapping(value = "/add.htm")
	@ResponseBody
	public JSONObject add(AdminVO vo) {
		JSONObject json = new JSONObject();
		json.element("status", "fail");
		/* 先判断登录名是否重复 */
		List<Admin> adminList = this.adminService.getByLoginName(vo
				.getLoginName());
		if (adminList != null && adminList.size() > 0) {
			json.element("msg", "登录名重复");
			return json;
		}
		this.adminService.addAdmin(vo);

		json.element("status", "succ");
		return json;
	}

	/**
	 * 编辑管理员
	 */
	@RequestMapping(value = "/edit.htm")
	@ResponseBody
	public JSONObject edit(AdminVO vo) {
		JSONObject json = new JSONObject();
		json.element("status", "fail");
		List<Admin> adminList = this.adminService.getByLoginName(vo
				.getLoginName());
		if (adminList.size() > 0 && (adminList.get(0).getId() != vo.getId())) {
			json.element("msg", "登录名重复");
			return json;
		}
		this.adminService.editByAdminVO(vo);

		json.element("status", "succ");
		return json;
	}

	/**
	 * 禁用管理员
	 */
	@RequestMapping(value = "/disable.htm")
	@ResponseBody
	public JSONObject disable(Integer id) {
		JSONObject json = new JSONObject();
		json.element("status", "fail");
		this.adminService.disableById(id);

		json.element("status", "succ");
		return json;
	}
	
	/**
	 * 删除管理员
	 */
	@RequestMapping(value = "/delete.htm")
	@ResponseBody
	public JSONObject delete(Integer id) {
		JSONObject json = new JSONObject();
		json.element("status", "fail");
		this.adminService.deleteById(id);

		json.element("status", "succ");
		return json;
	}

	/**
	 * 重设登录密码
	 */
	@RequestMapping(value = "/resetPwd.htm")
	@ResponseBody
	public JSONObject resetPwd(Integer id, String pwd, String newPwd,
			String confirmPwd) {
		JSONObject json = new JSONObject();
		json.element("status", "fail");

		Admin admin = this.adminService.getById(id);
		if (!admin.getPassword().equals(DigestUtils.md5Hex(pwd))) {
			json.element("msg", "原密码错误");
			return json;
		}
		if(!confirmPwd.equals(newPwd)) {
			json.element("msg", "新密码和确认密码不一致");
			return json;
		}
		this.adminService.resetPwd(id, newPwd);
		
		json.element("status", "succ");
		return json;
	}
}
